home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / dbwrendr.zip / SOURCE / RAY.H < prev    next >
Text File  |  1989-10-31  |  18KB  |  654 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1987, David B. Wecker                 *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * This file is part of DBW_Render                                      *
  7.  *                                                                      *
  8.  * DBW_Render is distributed in the hope that it will be useful, but    *
  9.  * WITHOUT ANY WARRANTY. No author or distributor accepts               *
  10.  * responsibility to anyone for the consequences of using it or for     *
  11.  * whether it serves any particular purpose or works at all, unless     *
  12.  * he says so in writing. Refer to the DBW_Render General Public        *
  13.  * License for full details.                                            *
  14.  *                                                                      *
  15.  * Everyone is granted permission to copy, modify and redistribute      *
  16.  * DBW_Render, but only under the conditions described in the           *
  17.  * DBW_Render General Public License. A copy of this license is         *
  18.  * supposed to have been given to you along with DBW_Render so you      *
  19.  * can know your rights and responsibilities. It should be in a file    *
  20.  * named COPYING. Among other things, the copyright notice and this     *
  21.  * notice must be preserved on all copies.                              *
  22.  ************************************************************************
  23.  *                                                                      *
  24.  * Authors:                                                             *
  25.  *      DBW - David B. Wecker                                           *
  26.  *      jhl - John H. Lowery (IBM conversion)                           *
  27.  *                                                                      *
  28.  * Versions:                                                            *
  29.  *      V1.0  870125 DBW - First released version                       *
  30.  *      V1.01 880918 jhl - ported to IBM PC (MCGA/VGA display hardware) *
  31.  *            890121 jhl - MAXROW & MAXCOL become variables, add        *
  32.  *                         'D' command for Display max <hor> <vert>     *
  33.  *                                                                      *
  34.  ************************************************************************/
  35.  
  36. #define IBM_PC 1      /* V1.01       */
  37.  
  38. #include <math.h>
  39. #include <stdio.h>
  40. #include <setjmp.h>
  41.  
  42. #ifdef IBM_PC
  43. #include <malloc.h>
  44. #endif
  45.  
  46. #define VERSION "RAY v1.02 891031 (IBM PC/XT/AT)\n Copyright (C) 1989 J. Lowery and D. Wecker - all rights reserved\n"
  47.  
  48. #define pi 3.141592654
  49.  
  50. #define X 0
  51. #define Y 1
  52. #define Z 2
  53. #define W 3
  54. #define B 0
  55. #define G 1
  56. #define R 2
  57.  
  58. #define minimum( a, b ) ( ((a) < (b)) ? (a) : (b) )
  59. #define maximum( a, b ) ( ((a) > (b)) ? (a) : (b) )
  60.  
  61. typedef float vector[3];
  62. typedef float matrix[4][4];
  63.  
  64. typedef struct 
  65. {
  66.      float xmin, xmax;
  67.      float ymin, ymax;
  68. }
  69. window;
  70.  
  71. #define setwindow( xmin, ymin, xmax, ymax, w ) {\
  72.     w.xmin = xmin;\
  73.     w.ymin = ymin;\
  74.     w.xmax = xmax;\
  75.     w.ymax = ymax; }
  76.  
  77. #define copywindow( f, w ) {\
  78.     w.xmin = f.xmin;\
  79.     w.ymin = f.ymin;\
  80.     w.xmax = f.xmax;\
  81.     w.ymax = f.ymax; }
  82.  
  83. typedef struct 
  84. {
  85.      float xmin, xmax;
  86.      float ymin, ymax;
  87.      float zmin, zmax;
  88. }
  89. volume;
  90.  
  91. #define setvolume( xmin, ymin, zmin, xmax, ymax, zmax, v ) {\
  92.     v.xmin = xmin;\
  93.     v.ymin = ymin;\
  94.     v.zmin = zmin;\
  95.     v.xmax = xmax;\
  96.     v.ymax = ymax;\
  97.     v.zmax = zmax; }
  98.  
  99. #define copyvolume( f, v ) {\
  100.     v.xmin = f.xmin;\
  101.     v.ymin = f.ymin;\
  102.     v.zmin = f.zmin;\
  103.     v.xmax = f.xmax;\
  104.     v.ymax = f.ymax;\
  105.     v.zmax = f.zmax; }
  106.  
  107. typedef struct 
  108. {
  109.      vector vrp, vpn, vup, cop;
  110.      volume vol;
  111. }
  112. perspective_projection;
  113.  
  114. typedef struct 
  115. {
  116.      int tex;
  117.      float fuz, ref, idx;
  118.      vector tra, amb, dif;
  119. }
  120. attributes;
  121.  
  122. typedef struct 
  123. {
  124.      vector p;
  125.      float g;
  126.      int s;
  127. }
  128. fracvert;
  129.  
  130. #define CV(x,y,z,v)     v[0]=(x); v[1]=(y); v[2]=(z)
  131. #define VECZERO(v)      v[0] = 0.0; v[1] = 0.0; v[2] = 0.0
  132. #define VECCOPY(frm,to) to[0] = frm[0]; to[1] = frm[1]; to[2] = frm[2]
  133. #define VECDUMP(v,str)  printf("%s\t%5.3f %5.3f %5.3f\n",str,v[0],v[1],v[2])
  134. #define VECSUB(v1,v2,r) r[0] = v1[0] - v2[0]; r[1] = v1[1] - v2[1];\
  135.                         r[2] = v1[2] - v2[2]
  136. #define VECSUM(v1,v2,r) r[0] = v1[0] + v2[0]; r[1] = v1[1] + v2[1];\
  137.                         r[2] = v1[2] + v2[2]
  138. #define VECMUL(v1,v2,r) r[0] = v1[0] * v2[0]; r[1] = v1[1] * v2[1];\
  139.                         r[2] = v1[2] * v2[2]
  140. #define VECDIV(v1,v2,r) r[0] = v1[0] / v2[0]; r[1] = v1[1] / v2[1];\
  141.                         r[2] = v1[2] / v2[2]
  142. #define VECSCALE(s,v,r) r[0] = (s)*v[0]; r[1] = (s)*v[1]; r[2] = (s)*v[2]
  143. #define NORM(v)         ((float)sqrt((v[0]*v[0])+(v[1]*v[1])+(v[2]*v[2])))
  144. #define DOT(v1,v2)      ((v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]))
  145. #define CROSS(v1,v2,r)  r[0] = (v1[1] * v2[2]) - (v2[1] * v1[2]);\
  146.                         r[1] = (v1[2] * v2[0]) - (v2[2] * v1[0]);\
  147.                         r[2] = (v1[0] * v2[1]) - (v2[0] * v1[1])
  148. #define DIRECTION(f,t,d)        VECSUB(t,f,d); normalize(d)
  149. #define SPHERENORMAL(cent,p,n)  DIRECTION(cent,p,n);
  150. #define PLANENORMAL(ve,vp,n)    CROSS(vp,ve,n); normalize(n);
  151.  
  152. #define ERROR( str ) { fprintf( stderr, "%s\n", str ); exit(0); }
  153.  
  154. #ifndef IBM_PC
  155. extern char *malloc();
  156. #endif
  157.  
  158. extern long time();
  159.  
  160. #define CHECK_ALLOC( ptr, typ ) {\
  161.     if (!(ptr = (typ *) malloc( sizeof( typ )))) {\
  162.         ERROR( "MALLOC - out of memory" ); \
  163.         exit( 0 ); \
  164.         } \
  165.     }
  166.  
  167. #define CHECK_ALLOCA( ptr, typ, num ) {\
  168.     if (!(ptr = (typ *) malloc( sizeof( typ ) * num ))) {\
  169.         ERROR( "MALLOC - out of memory" ); \
  170.         exit( 0 ); \
  171.         } \
  172.     }
  173.  
  174. #define sptr( p ) ((sphere *) p)
  175. #define tptr( p ) ((triangle *) p)
  176. #define qptr( p ) ((quad *) p)
  177. #define rptr( p ) ((ring *) p)
  178. #define eptr( p ) ((extent *) p)
  179. #define cptr( p ) ((cylinder *) p)
  180.  
  181. #define EXTENT 0
  182. #define SPHERE 1
  183. #define TRIANGLE 2
  184. #define QUAD 3
  185. #define RING 4
  186. #define CYLINDER 5
  187.  
  188. typedef float hvector[4];
  189.  
  190. typedef struct NODE 
  191. {
  192.      struct NODE *next;
  193.      int kind;
  194.      attributes attr;
  195. }
  196. node;
  197.  
  198. typedef struct 
  199. {
  200.      node *next;
  201.      int kind;
  202.      attributes attr;
  203.      vector position, ve, vp;
  204. }
  205. quad;
  206.  
  207. typedef struct 
  208. {
  209.      node *next;
  210.      int kind;
  211.      attributes attr;
  212.      vector position, ve, vp;
  213. }
  214. triangle;
  215.  
  216. typedef struct 
  217. {
  218.      node *next;
  219.      int kind;
  220.      attributes attr;
  221.      vector position, ve, vp;
  222.      float minrad, maxrad;
  223. }
  224. ring;
  225.  
  226. typedef struct 
  227. {
  228.      node *next;
  229.      int kind;
  230.      attributes attr;
  231.      vector center;
  232.      float radius;
  233. }
  234. sphere;
  235.  
  236. typedef struct 
  237. {
  238.      node *next;
  239.      int kind;
  240.      attributes attr;
  241.      vector bottom, top;
  242.      float a, b, c;
  243. }
  244. cylinder;
  245.  
  246. typedef struct 
  247. {
  248.      node *next;
  249.      int kind;
  250.      attributes attr;
  251.      vector base, apex;
  252.      float baserad;
  253. }
  254. cone;
  255.  
  256. typedef struct 
  257. {
  258.      node *next;
  259.      int kind;
  260.      attributes attr;
  261.      vector center;
  262.      float radius;
  263.      node *sub;
  264. }
  265. extent;
  266.  
  267. typedef struct 
  268. {
  269.      vector intensity;
  270.      vector direction;
  271.      float distscale, radius;
  272.      int kind;
  273. }
  274. lighttype;
  275.  
  276. typedef struct 
  277. {
  278.      vector center;
  279.      float wavelength, amplitude, drag, propagate;
  280. }
  281. wavetype;
  282.  
  283. typedef struct 
  284. {
  285.      vector color;
  286.      float distscale;
  287. }
  288. hazetype;
  289.  
  290. typedef struct 
  291. {
  292.      vector color;
  293.      float start, scale;
  294. }
  295. blendtype;
  296.  
  297. typedef struct 
  298. {
  299.      float start, altscale, altfactor, threshhold;
  300. }
  301. snowtype;
  302.  
  303. typedef struct 
  304. {
  305.      float scale, zoom;
  306. }
  307. pebbletype;
  308.  
  309. typedef struct 
  310. {
  311.      int level;
  312.      float xscale, yscale, zscale;
  313.      int texture;
  314. }
  315. fractaltype;
  316.  
  317. typedef struct 
  318. {
  319.      vector color;
  320.      float x, y, z, bevel, angle;
  321.